/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.server.check.checkers;

import cn.ac.iie.di.ban.news.server.check.CheckResult;
import cn.ac.iie.di.ban.news.server.check.Checker;

/**
 *
 * @author zwlin
 */
public class PhoneChecker implements Checker<String, String> {

	@Override
	public CheckResult check(String src, String target) {
		if (src == null || (src = src.trim()).isEmpty()) {
			return new CheckResult();
		}
		if (src.startsWith("+")) {
			src = src.substring(1);
		}
		if (!src.startsWith("86")) {
			return new CheckResult("no '86' prefix in the phone number.");
		}
		char[] phone = src.substring(2).toCharArray();
		if (phone.length != 11) {
			return new CheckResult("invalid phone number length.");
		}
		for (char c : phone) {
			if (c < '0' || c > '9') {
				return new CheckResult("not all characters are numbers in the phone number.");
			}
		}
		return new CheckResult();
	}

}
